
#ifndef TEXTFORMAT_H
#define TEXTFORMAT_H

#include <BMap>
#include <BRange>
#include <BTextFormat>

using namespace BWE;

Align StringToAlign(const BString& text);
BString StringFromAlign(Align align);

BString StringFromColor(const BColor& color);
BColor StringToColor(const BString& text);

BString StringFromFont(const BFont* font);
BFont* StringToFont(const BString& text);

struct CodeItem
{
	int			count = 0;
	BFontHolder	font;
	BColor		color;
	BColor		ground;
};
struct LineItem
{
	Align	align = Align_None;
	int		height = 0;
	int		spacing = 0;
};

class TextFormat : public BTextFormat
{
public:
	TextFormat();
	~TextFormat();

	virtual void styled(const BStyle* style);
	virtual void changed(const BString& text);

	virtual const BFont* font(int pos) const;
	virtual const BColor* color(int pos) const;
	virtual const BColor* ground(int pos) const;

	virtual Align lineAlign(int line) const;
	virtual int lineSpacing(int line) const;
	virtual int lineHeight(int line) const;

	void setLineAlign(int line, Align align);
	void setLineSpacing(int line, int spacing);
	void setLineHeight(int line, int height);

	void setFont(int pos, int count, BFont* font);
	void setColor(int pos, int count, const BColor& color);
	void setGround(int pos, int count, const BColor& ground);

	const BMap<int, CodeItem>& codeItemMap() const;
	const BMap<int, LineItem>& lineItemMap() const;

	void clear();

protected:
	void slotTextEditOpera(BWidget* widget, const BValue& value);
	void slotTextSelectionChanged(BWidget* widget, const BValue& value);

protected:
	virtual void attached(BWidget* host);
	virtual void detached(BWidget* host);

private:
	BMap<int, CodeItem>		_codeItemMap;
	BMap<int, LineItem>		_lineItemMap;
};

#endif
